package com.smartalarmclock.Logic;

import java.sql.Time;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Set;

import com.smartalarmclock.Model.ContactFactory;
import com.smartalarmclock.Model.IAutoResponse;
import com.smartalarmclock.Model.IContact;
import com.smartalarmclock.Model.IProfile;
import com.smartalarmclock.Model.ISingleContact;
import com.smartalarmclock.Model.ITimeFrame.DayInWeek;
import com.smartalarmclock.Model.ITimeFrame.TimeInWeek;
import com.smartalarmclock.Model.RulesItemFactory;
import com.smartalarmclock.Model.Exceptions.ConflictingSchedueledProfilesException;
import com.smartalarmclock.Model.Exceptions.DataEntityDoesNotExistException;
import com.smartalarmclock.Model.Exceptions.PhoneContactDoesNotExistException;

import android.content.Context;
import android.util.Log;

public class AcAutoResponseService {
	
	public static class AcAutoResponseServiceStatic {
		
		private static final String TAG = "AcAutoResponseServiceStatic";
		public AcAutoResponseServiceStatic() {
			super();
		}
		
		public static IAutoResponse GetAutoResponseForContact(String i_PhoneNumber, Context i_Context)
		{
			Log.v(TAG, "Entered getAutoResponseForContact. Phone: " + i_PhoneNumber);
			IProfile runningProfile = null;
			IAutoResponse autoResponse = null;
			try {
				runningProfile = AcManager.GetInstance().GetRunningProfile(i_Context, false);
			} catch (ConflictingSchedueledProfilesException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			ISingleContact contactCalling = null;
			ContactFactory contactFactory = new ContactFactory();
			try {
				contactCalling = contactFactory.GetSingleContactByPhoneNumber(i_Context, i_PhoneNumber);
			} catch (DataEntityDoesNotExistException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (PhoneContactDoesNotExistException e1) {
				Log.v(TAG, "Contact doesn't exist in phonebook");
				contactCalling = null;
			}
			
			if (runningProfile != null)
			{
				if (contactCalling == null || !runningProfile.GetContacts(i_Context).contains(contactCalling))
				{
					// For debug?
					String message = contactCalling == null ? "contact doesn't appear in phone" :
						"contact is not part of running profile...";
					Log.v(TAG, message);
					autoResponse = runningProfile.GetNonMatchingAutoResponse();
				}
				else
				{
					autoResponse = runningProfile.GetMatchingAutoResponse();
				}
			}
			
			return autoResponse;
		}	
	}
}
